﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerPublic.Data;
using ServerGame.GameObject;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;
using System.Text;
using System.Linq;

namespace ServerGame.Manager
{
    /// <summary>
    /// 管理 Efun
    /// </summary>
    public static partial class Efun
    {
        public static void Message(string text)
        {
            var objMsg = new G2C_Game_Message()
            {
                Text = text,
            };
            Send(objMsg);
        }
        public static void Message(StringBuilder sb)
        {
            Message(sb.ToString());
        }
        public static void Message(this Player player, string text)
        {
            var objMsg = new G2C_Game_Message()
            {
                Text = text,
            };
            player.Send(objMsg);
        }
        public static void Message(this MudObj mo, string text, IEnumerable<MudObj> listexclude = null)
        {
            switch (mo.Type)
            {
                case EMudObjType.Player:
                    ((Player)mo).Message(text);
                    break;
                case EMudObjType.Npc:
                    break;
                case EMudObjType.Item:
                    break;
                case EMudObjType.Equip:
                    break;
                case EMudObjType.Room:
                    RoomMessage((Room)mo, text, listexclude);
                    break;
                default:
                    break;
            }
        }
        //房间广播
        public static void RoomMessage(this Room room, string text, MudObj exclude = null)
        {
            if (exclude == null)
            {
                foreach (var pp in room.DictPlayer.Values)
                {
                    pp.Message(text);
                }
            }
            else
            {
                RoomMessage(room, text, new MudObj[] { exclude });
            }
        }
        public static void RoomMessage(this Room room, string text, IEnumerable<MudObj> listexclude)
        {
            if (listexclude == null)
            {
                foreach (var pp in room.DictPlayer.Values)
                {
                    pp.Message(text);
                }
            }
            else
            {
                var listpuid = (from it in listexclude
                                select it.Uid);
                foreach (var pp in room.DictPlayer.Values)
                {
                    if (listpuid.Contains(pp.Puid))
                    {
                        continue;
                    }
                    pp.Message(text);
                }
            }
        }
        //房间广播
        public static void RoomMessage(this Room room, ProtocolMsgBase msg)
        {
            foreach (var pp in room.DictPlayer.Values)
            {
                pp.Send(msg);
            }
        }
        public static void MessageVision(string msg, MudObj me, MudObj you = null)
        {
            string str1, str2, str3;
            var my_name = me.MudName();
            var my_gender = me.MudGender();

            str1 = msg.Replace("$P", GenderSelf(my_gender));
            str1 = str1.Replace("$N", GenderSelf(my_gender));
            str3 = msg.Replace("$P", my_name);
            str3 = str3.Replace("$N", my_name);

            if (you != null)
            {
                var your_name = you.MudName();
                var your_gender = you.MudGender();
                var your_unit = you.MudUnit();
                str2 = msg.Replace("$P", GenderPronoun(my_gender));
                str2 = str2.Replace("$p", GenderSelf(your_gender));
                str2 = str2.Replace("$N", my_name);
                str2 = str2.Replace("$n", GenderSelf(your_gender));
                you.Message(str2);
                str1 = str1.Replace("$p", GenderPronoun(your_gender));
                str1 = str1.Replace("$n", your_name);
                str1 = str1.Replace("$u", your_unit);
                str3 = str3.Replace("$p", your_name);
                str3 = str3.Replace("$n", your_name);
                str3 = str3.Replace("$u", your_unit);
            }
            me.Message(str1);

            if (me.GetEnv(out var evn))
            {
                if (you == null)
                {
                    evn.Message(str3, new MudObj[] { me });
                }
                else
                {
                    evn.Message(str3, new MudObj[] { me, you });
                }
            }
        }
    }
}
